package stsj.meddroid;

import stsj.meddroid.screen.BattleScreen;
import stsj.meddroid.screen.CreditScreen;
import stsj.meddroid.screen.MainMenuScreen;
import stsj.meddroid.screen.RoundSelectScreen;
import stsj.meddroid.screen.SplashScreen;
import stsj.meddroid.screen.StageSelectScreen;
import stsj.meddroid.screen.StoryScreen;
import stsj.meddroid.screen.ViroScreen;

import android.content.Context;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;

public class MainAppListener extends Game {

	public final static int SPLASH_SCREEN = 0;
	public final static int MAIN_MENU = 1;
	public final static int STAGE_SELECTION = 10;
	public final static int STAGE_1_ROUND = 11;
	public final static int STAGE_2_ROUND = 12;
	public final static int STAGE_3_ROUND = 13;
	public final static int STAGE_4_ROUND = 14;
	public final static int STAGE_5_ROUND = 15;
	public final static int CREDIT = 3;
	public final static int BATTLE = 40;
	public final static int BATTLE_1 = 41;
	public final static int BATTLE_2 = 42;
	public final static int BATTLE_3 = 43;
	public final static int BATTLE_4 = 44;
	public final static int BATTLE_5 = 45;
	public final static int BATTLE_END = 46;
	
	public final static int IN_GAME_MENU = 6;
	public final static int VIRO = 7;
	public final static int STORY_1 = 81;
	public final static int STORY_2 = 82;
	public final static int STORY_3 = 83;
	public final static int STORY_4 = 84;
	public final static int STORY_5 = 85;
	public static final int STORY_6 = 86;
	public static final int STORY_7 = 87;
	
	public Context mcontext;


	public SplashScreen splashScreen;
	public MainMenuScreen mainMenuScreen;
	public CreditScreen creditScreen;
	public StageSelectScreen stageScreen;
	public ViroScreen viroScreen;

	public BattleScreen battleScreen;
	public StoryScreen storyScreen;
	public RoundSelectScreen roundScreen;
	
	public static int state = SPLASH_SCREEN;
	
	public MainAppListener(Context context) {
		// TODO Auto-generated constructor stub
		mcontext = context;
	}
	
	@Override
	public void create() {
		// load settings di sini..
		Settings.load();
		Gdx.app.log("APP ", "CREATE");
		// first show
		if (state == SPLASH_SCREEN) {
			splashScreen = new SplashScreen(this);
			setScreen(splashScreen);
		} else {
			setState(MAIN_MENU);
		}
		
		
		
		
		
		
	}

	public void initializeAllScreens() {
		stageScreen =  new StageSelectScreen(this);
		mainMenuScreen =  new MainMenuScreen(this);
		roundScreen = new RoundSelectScreen(this);
		viroScreen = new ViroScreen(this);
		storyScreen = new StoryScreen(this,0);
		creditScreen = new CreditScreen(this);
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
		Resources.pauseAllMusics();
		Settings.save();
		
	}

	@Override
	public void render() {
		// TODO Auto-generated method stub
		super.render();


	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		super.resize(width, height);
//		Gdx.app.log("APP Resize", "YES");
	}

	@Override
	public void resume() {
		super.resume();
//		Gdx.app.log("APP Resume", "YES");
//		Resources.resumeAllMusics();
	}

	@Override
	public void dispose() {
		super.dispose();
//		Gdx.app.log("APP Dispose", "YES");
		Settings.save();
		stageScreen.dispose();
		mainMenuScreen.dispose();
		roundScreen.dispose();
	}



	public void setState(int state) {
		// ngeset state dan sekalian pindah screen...
		this.state = state; // ngeset state 
		
		switch (state) {
		case MAIN_MENU: {
			setScreen(mainMenuScreen);
			break;
		}
		case VIRO: {
			setScreen(viroScreen);
			break;
		}
		case CREDIT: {
			setScreen(creditScreen);
			break;
		}
		
		case STAGE_1_ROUND: {
			roundScreen.currentStage=1;
			setScreen(roundScreen);
			break;
		}
		case STAGE_2_ROUND: {
			roundScreen.currentStage=2;
			setScreen(roundScreen);
			break;
		}
		case STAGE_3_ROUND: {
			roundScreen.currentStage=3;
			setScreen(roundScreen);
			break;
		}
		case STAGE_4_ROUND: {
			roundScreen.currentStage=4;
			setScreen(roundScreen);
			break;
		}
		case STAGE_5_ROUND: {
			roundScreen.currentStage=5;
			setScreen(roundScreen);
			break;
		}
		case STAGE_SELECTION: {
			setScreen(stageScreen);
			break;
		}
		case BATTLE_1 :{
			setScreen(battleScreen);
			break;
		}
		case BATTLE_2 :{
			setScreen(battleScreen);
			break;
		}
		case BATTLE_3 :{
			setScreen(battleScreen);
			break;
		}
		case BATTLE_4 :{
			setScreen(battleScreen);
			break;
		}
		case BATTLE_5 :{
			setScreen(battleScreen);
			break;
		}
		case BATTLE_END :{
			setScreen(battleScreen);
			break;
		}
		
		case STORY_1 :{
			storyScreen.storyNumber = 1;
			setScreen(storyScreen);
			break;
		}
		case STORY_2 :{
			storyScreen.storyNumber = 2;
			setScreen(storyScreen);
			break;
		}
		case STORY_3 :{
			storyScreen.storyNumber = 3;
			setScreen(storyScreen);
			break;
		}
		case STORY_4 :{
			storyScreen.storyNumber=4;
			setScreen(storyScreen);
			break;
		}
		case STORY_5 :{
			storyScreen.storyNumber = 5;
			setScreen(storyScreen);
			break;
		}
		case STORY_6 :{
			storyScreen.storyNumber = 6;
			setScreen(storyScreen);
			break;
		}
		case STORY_7 :{
			storyScreen.storyNumber = 7;
			setScreen(storyScreen);
			break;
		}
		
		
		}
	}
	public void startBattle( int stage ,int round, Screen lastScreen){
		  
//		if (battleScreen == null){
			battleScreen = new BattleScreen(this, lastScreen);
//			battleScreen.initResources();
//		}
		
//		battleScreen.lastScreen = lastScreen;
		if(stage == 6){
			battleScreen.initRound(5,5);
			setState(BATTLE_END);
		}else{
			battleScreen.initRound(stage,round);
			setState(BATTLE + stage);
		}
		
	}
	
	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
//		Gdx.app.log("APP", "Finalize");
	}

	public void exitGame(){	
		Resources.releaseAllResources();
		dispose();
		System.gc();
		System.exit(0);
		
	}
	
	
}
